(function () {
  "use strict";

  // custom scrollbar
  $("html").niceScroll({
    styler: "fb",
    cursorcolor: "#65cea7",
    cursorwidth: '6',
    cursorborderradius: '0px',
    background: '#424f63',
    spacebarenabled: false,
    cursorborder: '0',
    zindex: '1000'
  });

  $(".left-side").niceScroll({
    styler: "fb",
    cursorcolor: "#65cea7",
    cursorwidth: '3',
    cursorborderradius: '0px',
    background: '#424f63',
    spacebarenabled: false,
    cursorborder: '0'
  });

  $(".left-side").getNiceScroll();
  if ($('body').hasClass('left-side-collapsed')) {
    $(".left-side").getNiceScroll().hide();
  }

  // Toggle Left Menu
  jQuery('body').delegate('.menu-list > a', 'click',
    function () {

      var parent = jQuery(this).parent();
      var sub = parent.find('> ul');

      if (!jQuery('body').hasClass('left-side-collapsed')) {
        if (sub.is(':visible')) {
          sub.slideUp(200,
            function () {
              parent.removeClass('nav-active');
              jQuery('.main-content').css({
                height: ''
              });
              mainContentHeightAdjust();
            });
        } else {
          visibleSubMenuClose();
          parent.addClass('nav-active');
          sub.slideDown(200,
            function () {
              mainContentHeightAdjust();
            });
        }
      }
      return false;
    });

  function visibleSubMenuClose() {
    jQuery('.menu-list').each(function () {
      var t = jQuery(this);
      if (t.hasClass('nav-active')) {
        t.find('> ul').slideUp(200,
          function () {
            t.removeClass('nav-active');
          });
      }
    });
  }

  function mainContentHeightAdjust() {
    // Adjust main content height
    var docHeight = jQuery(document).height();
    if (docHeight > jQuery('.main-content').height()) jQuery('.main-content').height(docHeight);
  }

  //  class add mouse hover
  jQuery('.custom-nav > li').hover(function () {
      jQuery(this).addClass('nav-hover');
    },
    function () {
      jQuery(this).removeClass('nav-hover');
    });

  // Menu Toggle
  jQuery('.toggle-btn').click(function () {
    $(".left-side").getNiceScroll().hide();

    if ($('body').hasClass('left-side-collapsed')) {
      $(".left-side").getNiceScroll().hide();
    }
    var body = jQuery('body');
    var bodyposition = body.css('position');

    if (bodyposition != 'relative') {

      if (!body.hasClass('left-side-collapsed')) {
        body.addClass('left-side-collapsed');
        jQuery('.custom-nav ul').attr('style', '');

        jQuery(this).addClass('menu-collapsed');

      } else {
        body.removeClass('left-side-collapsed chat-view');
        jQuery('.custom-nav li.active ul').css({
          display: 'block'
        });

        jQuery(this).removeClass('menu-collapsed');

      }
    } else {

      if (body.hasClass('left-side-show')) body.removeClass('left-side-show');
      else body.addClass('left-side-show');

      mainContentHeightAdjust();
    }

  });

  searchform_reposition();

  jQuery(window).resize(function () {

    if (jQuery('body').css('position') == 'relative') {

      jQuery('body').removeClass('left-side-collapsed');

    } else {

      jQuery('body').css({
        left: '',
        marginRight: ''
      });
    }

    searchform_reposition();

  });

  function searchform_reposition() {
    if (jQuery('.searchform').css('position') == 'relative') {
      jQuery('.searchform').insertBefore('.left-side-inner .logged-user');
    } else {
      jQuery('.searchform').insertBefore('.menu-right');
    }
  }

  // panel collapsible
  $('.panel .tools .fa').click(function () {
    var el = $(this).parents(".panel").children(".panel-body");
    if ($(this).hasClass("fa-chevron-down")) {
      $(this).removeClass("fa-chevron-down").addClass("fa-chevron-up");
      el.slideUp(200);
    } else {
      $(this).removeClass("fa-chevron-up").addClass("fa-chevron-down");
      el.slideDown(200);
    }
  });

  $('.todo-check label').click(function () {
    $(this).parents('li').children('.todo-title').toggleClass('line-through');
  });

  $(document).on('click', '.todo-remove',
    function () {
      $(this).closest("li").remove();
      return false;
    });

  //$("#sortable-todo").sortable();

  // panel close
  $('.panel .tools .fa-times').click(function () {
    $(this).parents(".panel").parent().remove();
  });

  // tool tips
  $('.tooltips').tooltip();

  // popovers
  //$('.popovers').popover();
})(jQuery);

(function (global) {
  var $element = $('.page-tab-container');
  var handler = {
    pages: [],
    init: function () {
        var _this = this;
        $($element).delegate('li[new-tab]', 'click', function () {
          _this.active(_this.put());
          location.href='index.html#/_blank' ;
        });
        $($element).delegate('li[tab] label', 'click', function () {
          console.log('标签事件');
          var _ele = $(this).parent();
          _this.pages.forEach(function (v, k) {
            console.log($(_ele).attr('data-filter'), v['data-filter'], $(_ele).attr('data-filter') == v['data-filter'])
            if ($(_ele).attr('data-filter') == v['data-filter'])
              _this.active(v);
          });
        });
        this.active(this.put());
        this.addStyle();
        return this;
      },
      addStyle: function () {
        if ($('head').find('link[from=page-tab]').length)
          return this;
        $('<link>').attr({
          rel: 'stylesheet',
          type: 'text/css',
          href: 'assets/modules/ng-page-tab/style.css',
          from: 'page-tab'
        }).appendTo($('head'));
      },
      put: function (ops) {
        var container = $('#page-container'),
          tabContainer = $('.page-tab-container'),
          page = $('<div></div>');

        var tab = $('<li tab></li>'),
          label = $('<label></label>').text(ops && ops.name || '新标签').appendTo(tab),
          tabClose = $('<span>&times;</span>').addClass('close').appendTo(tab);

        var _this = this;
        tabClose.bind('click', function () {
          _this.remove($(this).parent().attr('data-filter'));
        });

        tabContainer.find('[new-tab]').before(tab);

        var tabUid = 'tab' + new Date().getTime() + Math.round(Math.random(1000000));
        tab.attr('data-filter', tabUid);

        var opage = {
          tab: tab,
          label: label,
          ops: ops,
          page: page,
          'data-filter': tabUid
        };
        this.pages.push(opage);

        return opage;
      },
      setLabel: function (label) {
        this.getCurrent().label.text(label || '新标签');
      },
      getCurrent: function () {
        var current, uid = $('.page-tab-container').find('li.active').attr('data-filter');
        return this.getWithUid(uid);
      },
      getWithUid: function (uid) {
        var current;
        this.pages.forEach(function (v, k) {
          if (v['data-filter'] == uid)
            current = v;
        });
        return current;
      },
      remove: function (uid) {
        console.log('删除标签', uid);
        var index = -1,
          _this = this;
        this.pages.forEach(function (v, k) {
          if (v['data-filter'] == uid)
            index = k;
        });

        if (uid == this.getCurrent()['data-filter']) {
          $('#page-container').find('[ui-view=main]').empty();
          if (this.pages.length == 1)
            this.active(this.put());
          else if (index == 0)
            this.active(this.pages[index + 1]);
          else
            this.active(this.pages[index - 1]);

        }
        this.pages[index].tab.remove();
        this.pages[index].page.remove();
        this.pages.splice(index, 1);


      },
      active: function (opage) {
        console.log('激活', opage);
        var container = $('#page-container'),
          tabContainer = $('.page-tab-container');

        var current = this.getCurrent();
        if (current)
          container.find('[ui-view=main]').children().appendTo(current.page);

        opage.page.children().appendTo(container.find('[ui-view=main]'));

        tabContainer.find('li.active').removeClass('active');
        opage['tab'].addClass('active');

        return opage;
      },
      close: function () {

      }
  }.init();
  global.pwin = {
    setLabel: function (label) {
        handler.setLabel(label);
      },
      close: function () {
        handler.close();
      }
  }
})(window);